package ui.activity;

import android.app.DatePickerDialog;
import android.content.Intent;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.fy.fyzf.R;
import com.fy.fyzf.base.BaseActivity;
import com.fy.fyzf.bean.AddSignThree;
import com.fy.fyzf.bean.PassengerNumberBean;
import com.fy.fyzf.bean.BaseData;
import com.fy.fyzf.bean.SaveBean;
import com.fy.fyzf.bean.SignDetailBean;
import com.fy.fyzf.presenter.AddSignPresenter;
import com.fy.fyzf.utils.AppUtils;
import com.fy.fyzf.utils.MathUtils;
import com.fy.fyzf.view.AddSignCallback;
import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import ui.adapter.CheckBoxAdapter;
import ui.adapter.FenPeiInfoAdapter;

public class AddSignThreeActivity extends BaseActivity<AddSignPresenter> implements AddSignCallback {


    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_contacts)
    EditText etContacts;
    @BindView(R.id.et_count)
    EditText etCount;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.recycleView1)
    RecyclerView recycleView1;
    @BindView(R.id.et_owner_comm)
    EditText etOwnerComm;
    @BindView(R.id.tv_payment_time)
    TextView tvPaymentTime;
    @BindView(R.id.et_selef_performance)
    EditText etSelefPerformance;
    @BindView(R.id.et_rebate)
    EditText etRebate;
    @BindView(R.id.recycleView2)
    RecyclerView recycleView2;
    @BindView(R.id.et_remark)
    EditText etRemark;
    @BindView(R.id.recycleView3)
    RecyclerView recycleView3;
    @BindView(R.id.tv_last_step)
    TextView tvLastStep;
    @BindView(R.id.tv_next)
    TextView tvNext;

    private AddSignThree addSignThree = new AddSignThree();
    List<SignDetailBean.PerformanceUserDetailsVosBean> signList = new ArrayList<>();
    private FenPeiInfoAdapter fenPeiInfoAdapter;
    private int customersId;
    private int performanceId;
    private int signType;

    @Override
    protected AddSignPresenter createPresenter() {
        return new AddSignPresenter(this);
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_add_sign_three;
    }

    @Override
    public void initView() {
        super.initView();
        setTitleName("签约详情");
        tvNext.setText("保存");
        CheckBoxAdapter checkBoxAdapter1 = new CheckBoxAdapter(this);
        recycleView1.setLayoutManager(new GridLayoutManager(this,3));
        recycleView1.setAdapter(checkBoxAdapter1);

        List<String> titles = new ArrayList<>();
        titles.add("老顾客及朋友介绍");
        titles.add("网络");
        titles.add("个人客户");
        titles.add("渠道客户");
        titles.add("合作客户");
        checkBoxAdapter1.setNewData(titles);
        addSignThree.setCustomerSource(1);
        checkBoxAdapter1.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                checkBoxAdapter1.setSelect(position);
                addSignThree.setCustomerSource(position +1);
            }
        });


        CheckBoxAdapter checkBoxAdapter2 = new CheckBoxAdapter(this);
        recycleView2.setLayoutManager(new GridLayoutManager(this,3));
        recycleView2.setAdapter(checkBoxAdapter2);

        List<String> titles2 = new ArrayList<>();
        titles2.add("工行");
        titles2.add("中行");
        titles2.add("支付宝");
        titles2.add("微信");
        titles2.add("其他");

        checkBoxAdapter2.setNewData(titles2);
        addSignThree.setCommissionPayType(1);
        checkBoxAdapter2.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                checkBoxAdapter2.setSelect(position);
                addSignThree.setCommissionPayType(position +1);
            }
        });

        fenPeiInfoAdapter = new FenPeiInfoAdapter(1);
        recycleView3.setLayoutManager(new LinearLayoutManager(this));
        recycleView3.setAdapter(fenPeiInfoAdapter);

        signList.add(new SignDetailBean.PerformanceUserDetailsVosBean("","","","","",1));
        fenPeiInfoAdapter.setNewData(signList);
        fenPeiInfoAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.tv_add) {
                    signList.add(new SignDetailBean.PerformanceUserDetailsVosBean("","","","","",1));
                    fenPeiInfoAdapter.insertItem(fenPeiInfoAdapter.getItemCount() - 1);
                }
            }
        });


    }

    @Override
    public void initData() {
        super.initData();
        customersId = getIntent().getIntExtra("customersId", -1);
        performanceId = getIntent().getIntExtra("performanceId", -1);
        signType = getIntent().getIntExtra("type", -1);

        if (signType == 2) {
            mPresenter.getSingnContractdetail(performanceId);
        }
    }

    @Override
    public void getPassengerNumberListSuccess(List<PassengerNumberBean> list) {

    }

    @Override
    public void saveOneStepSuccess(BaseData baseData) {

    }

    @Override
    public void saveTwoStepSuccess(BaseData baseData) {

    }

    @Override
    public void saveThreeStepSuccess(BaseData baseData) {
        AppUtils.showToast("保存成功");
        EventBus.getDefault().post("saveSuccess");
        SaveBean saveBean = new Gson().fromJson(baseData.data.toString(), SaveBean.class);
        String performanceId = saveBean.getPerformanceId();
        String s = MathUtils.rvZeroAndDot(performanceId);
//        SPUtils.getInstance(this).putString("performanceId", s);

        finish();
    }

    @Override
    public void getSingnContractdetail(SignDetailBean signContractDetailBean) {
        etName.setText(signContractDetailBean.getCustomerName());
        addSignThree.setCustomerName(signContractDetailBean.getCustomerName());
        etContacts.setText(signContractDetailBean.getCustomerContact());
        addSignThree.setCustomerContact(signContractDetailBean.getCustomerContact());

        etCount.setText(signContractDetailBean.getCustomerCheckNumber());
        addSignThree.setCustomerCheckNumber(signContractDetailBean.getCustomerCheckNumber());
        etPhone.setText(signContractDetailBean.getCustomerPhone());
        addSignThree.setCustomerPhone(signContractDetailBean.getCustomerPhone());

        etOwnerComm.setText(signContractDetailBean.getCommissionOwner());
        addSignThree.setCommissionOwner(signContractDetailBean.getCommissionOwner());
        tvPaymentTime.setText(signContractDetailBean.getCommissionPayTime());
        addSignThree.setCommissionPayTime(signContractDetailBean.getCommissionPayTime());
        addSignThree.setCommissionResults(signContractDetailBean.getCommissionResults());
        etSelefPerformance.setText(signContractDetailBean.getCommissionResults());
        etRebate.setText(signContractDetailBean.getCommissionHold());
        addSignThree.setCommissionHold(signContractDetailBean.getCommissionHold());
        addSignThree.setRemark(signContractDetailBean.getRemark());
        etRemark.setText(signContractDetailBean.getRemark());

    }

    @Override
    public void exChangeSignContractSuccess(BaseData data) {

    }


    @RequiresApi(api = Build.VERSION_CODES.N)
    @OnClick({R.id.tv_payment_time,R.id.tv_last_step, R.id.tv_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_payment_time:
                showDateDialog();
                break;
            case R.id.tv_last_step:
                Intent intent = new Intent(this,AddSignTwoActivity.class);
                intent.putExtra("customersId", customersId);
                intent.putExtra("performanceId", performanceId);
                intent.putExtra("type", signType);
                startActivity(intent);
                break;
            case R.id.tv_next:

                if (TextUtils.isEmpty(etName.getText().toString())) {
                    AppUtils.showToast("姓名不能为空");
                    return;
                }
                if (TextUtils.isEmpty(etContacts.getText().toString())) {
                    AppUtils.showToast("联系人不能为空");
                    return;
                }

                if (TextUtils.isEmpty(etCount.getText().toString())) {
                    AppUtils.showToast("看房次数不能为空");
                    return;
                }
                if (TextUtils.isEmpty(etPhone.getText().toString())) {
                    AppUtils.showToast("电话号码不能为空");
                    return;
                }

                if (TextUtils.isEmpty(etOwnerComm.getText().toString())) {
                    AppUtils.showToast("业主佣金不能为空");
                    return;
                }
                if (TextUtils.isEmpty(tvPaymentTime.getText().toString())) {
                    AppUtils.showToast("付款时间不能为空");
                    return;
                }
                if (TextUtils.isEmpty(etSelefPerformance.getText().toString())) {
                    AppUtils.showToast("个人业绩不能为空");
                    return;
                }

                if (TextUtils.isEmpty(etRebate.getText().toString())) {
                    AppUtils.showToast("合作返佣不能为空");
                    return;
                }
                addSignThree.setCommissionHold(etRebate.getText().toString());
                addSignThree.setCommissionOwner(etOwnerComm.getText().toString());
                addSignThree.setCommissionResults(etSelefPerformance.getText().toString());
                addSignThree.setCustomerCheckNumber(etCount.getText().toString());
                addSignThree.setCustomerContact(etContacts.getText().toString());
                addSignThree.setCustomerName(etName.getText().toString());
                addSignThree.setCustomerPhone(etPhone.getText().toString());
//                String performanceId = SPUtils.getInstance(this).getString("performanceId", "");
                addSignThree.setPerformanceId(performanceId);
                addSignThree.setRemark(etRemark.getText().toString());

                List<SignDetailBean.PerformanceUserDetailsVosBean> data = fenPeiInfoAdapter.getData();

                List<AddSignThree.PerformanceUserParamsBean> performanceUserParams = new ArrayList<>(data.size());

                for (int i = 0; i < data.size(); i++) {
                    performanceUserParams.add(new AddSignThree.PerformanceUserParamsBean(data.get(i).getAssignGroupName(),data.get(i).getAssignName(),data.get(i).getAssignPct(),data.get(i).getAssignReason(),data.get(i).getAssignTurnover(),null));
                }

                addSignThree.setPerformanceUserParams(performanceUserParams);
                Log.e("addSignThree", new Gson().toJson(addSignThree));
                mPresenter.saveThreeStep(addSignThree);
                break;
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    private void showDateDialog() {
        //获取日历的一个实例，里面包含了当前的年月日
        Calendar calendar = Calendar.getInstance();
        DatePickerDialog dialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                tvPaymentTime.setText(year + "-" + month + "-" + dayOfMonth);
                addSignThree.setCommissionPayTime(tvPaymentTime.getText().toString() +" 00:00:00");
//                stringBuilderDate.append(year + "-" + month + "-" + dayOfMonth);
            }
        },
                calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH));
        dialog.show();

    }
}